package com.zhengshang.api.controller.goodsshop;

import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.core.domain.model.LoginAppUser;
import com.zhengshang.security.service.TokenService;
import com.zhengshang.third.jingdong.JingDongUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "基础模块-京东接口")
@Controller
@RequestMapping("/jingdong")
public class AppJingDongController {

    @Autowired
    private JingDongUtils jingDongUtils;

    @Autowired
    private TokenService tokenService;


    /**
     * 获取授权Url
     */
    @GetMapping(value = "/getAppOauthUrl")
    @ResponseBody
    public AjaxResult getAppOauthUrl(String redirectUri, String state) {
        String oauthUrl = jingDongUtils.getAppOauthUrl(redirectUri, state);
        return AjaxResult.success("获取成功", oauthUrl);
    }

    /**
     * 获取accessToken
     */
    @GetMapping(value = "/getAccessToken")
    @ResponseBody
    public AjaxResult getAccessToken(HttpServletRequest request, String code) {
        String accessToken = jingDongUtils.getAccessToken(code);
        return AjaxResult.success("获取成功", accessToken);
    }


    /**
     * 物料搜索
     */
    @GetMapping(value = "/goodsSearch")
    @ResponseBody
    public AjaxResult goodsSearch(String accessToken, Integer pageIndex, String goodsName, String sortName, String sort, Integer isCoupon) {
        return jingDongUtils.goodsQuery(accessToken, pageIndex, goodsName, sortName, sort, isCoupon);
    }


    /**
     * 获取推广链接
     */
    @ApiOperation(value = "获取推广链接")
    @GetMapping("/getGoodsPromoteUrl")
    @ResponseBody
    public AjaxResult getGoodsPromoteUrl(HttpServletRequest request, String accessToken, String materialId, String couponUrl) {
        LoginAppUser loginAppUser = tokenService.getLoginAppUser(request);
        Long userId = loginAppUser.getAppUser().getUserId();
        return jingDongUtils.getGoodsPromoteUrl(accessToken, userId, materialId, couponUrl);
    }


}
